package com.power.mediators
{
	import com.power.enum.ConfigUrl;
	import com.power.enum.NotificationTypes;
	import com.power.events.AppEvent;
	import com.power.events.HouseFloorEvent;
	import com.power.interfaces.IViewMediator;
	import com.power.model.FloorVo;
	import com.power.model.Notification;
	import com.power.model.factories.FloorFactory;
	import com.power.views.FloorWindowView;
	import com.power.views.components.FloorButton;
	import com.power.views.components.FloorTag;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	public class FloorWindowViewMediator implements IViewMediator
	{
		private var _floorView:FloorWindowView;
		
		private var _floorCategroys:Array;
		
		private var _floorVos:Vector.<FloorVo>;
		
		private var floorFactory:FloorFactory
		
		private var _houserMediator:HouseWindowViewMediator;
		
		public function FloorWindowViewMediator()
		{
			
		}
		
		public function initView(view:DisplayObject):void
		{
			_floorView = view as FloorWindowView;
			setAllEventListener();
		}
		public function initFloorView(view:DisplayObject,houserMediator:HouseWindowViewMediator):void
		{
			_floorView = view as FloorWindowView;
			_houserMediator = houserMediator;
			setAllEventListener();
		}
		
		private function setAllEventListener():void
		{
			_floorView.addEventListener(AppEvent.APP_EVENT,onAppEventHandler);
			_floorView.addEventListener(HouseFloorEvent.FLOOR_LIST_EVENT,changeHouseHandler);
		}
		public function onAppEventHandler(e:AppEvent):void
		{
			var notification:Notification = e.notification;
			handleNotification(notification);
		}
		public function changeHouseHandler(e:HouseFloorEvent):void
		{
			//trace("111111111111111111111");
			_houserMediator.handleNotification(e.notification);
		}
		public function initCategroys(cates:Array):void
		{
			_floorCategroys = cates;
			for each(var label:String in _floorCategroys)
			{
				var floorBtn:FloorButton = new FloorButton();
				floorBtn.setButtonLabel(label);
				_floorView.floorlist.addElement(floorBtn);
			}
		}
		public function initFloorTags(floorvos:Vector.<FloorVo>):void
		{
			_floorView.floorTags.removeAllElements();
			for each(var vo:FloorVo in floorvos)
			{
				var floorTag:FloorTag = new FloorTag();
				floorTag.setFloorVo(vo);
				_floorView.floorTags.addElement(floorTag);
			}
		}
		private function changeFloorCategorys(floorvos:Vector.<FloorVo>):void
		{
			
		}
		public function dispatchEvent(type:String, data:Object):void
		{
			
		}
		
		public function handleNotification(notification:Notification):void
		{
			var nofi_type:String = notification.type;
			switch(nofi_type)
			{
				case NotificationTypes.CHANGE_FLOOR_CAEGRORYS: changeFloorCateGorys(notification);
			}
		}
		private function changeFloorCateGorys(notification:Notification):void
		{
			
			trace("changing floor categorys!");
			var floortype:String = notification.body.floortype;
			trace(floortype);
			//update the floor list
			floorFactory = new FloorFactory();
			floorFactory.startLoadFloorAssets(ConfigUrl.FLOOL_URL,floortype);
			floorFactory.addEventListener("ParserFloorDataEndByType",changeFloorPicListHandler);
			
			
		}
		public function changeFloorPicListHandler(e:Event):void
		{
			initFloorTags(floorFactory.getFloorData())
		}
		public function getFloorVos(vos:Vector.<FloorVo>):void
		{
			_floorVos = vos;
			
		} 
		
		public function hide():void
		{
		}
		
		public function show():void
		{
		}
	}
}